/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package percussiongenerator.input;

import java.util.ArrayList;
import org.jfonia.model.PerformanceNode;
import org.jfonia.model.Staff;
import org.jfonia.model.ToneSequence;
import percussiongenerator.model.Pair;

/**
 *
 * @author Jannes Plyson
 */
public class InputStartEnd{       
    protected ArrayList<Pair<Long,Long>> times;
    protected int defaultPitch,defaultVelocity;

    public InputStartEnd(){
        times = new ArrayList<Pair<Long, Long>>();
    }

    public void setDefaultPitch(int pitch){
        defaultPitch = pitch;
    }

    public void setDefaultVelocity(int velocity){
        defaultVelocity = velocity;
    }

    public Staff getStaff(){
        Staff staff = new Staff();
        ToneSequence seq = new ToneSequence();
        staff.addSequence(seq);
        if(!times.isEmpty()){
            Pair<Long,Long> time = times.get(0);
            long start = time.first;
            PerformanceNode node = new PerformanceNode(time.second-time.first);
            node.setPitch(defaultPitch);
            node.setVelocity(defaultVelocity);
            seq.add(node);
            for(int i = 1; i < times.size(); i++){
                time = times.get(i);
                if(seq.getPerformanceLength() < time.first - start){
                    PerformanceNode silence = new PerformanceNode(time.first-start-seq.getPerformanceLength());
                    silence.setPitch(0);
                    silence.setVelocity(0);
                    seq.add(silence);
                }
                node = new PerformanceNode(time.second-time.first);
                node.setPitch(defaultPitch);
                node.setVelocity(defaultVelocity);
                seq.add(node);
            }
        }
        seq.setLockNotation(false);
        return staff;
    }
    
    public void addInput(long nanoTimeStart, long nanoTimeEnd){
        times.add(new Pair<Long, Long>(nanoTimeStart,nanoTimeEnd));
    }
    
    public void addInput(double secondsStart, double secondsEnd){
        long nanoTimeStart = (long)(secondsStart*1000000000);
        long nanoTimeEnd = (long)(secondsEnd*1000000000);
        addInput(nanoTimeStart,nanoTimeEnd);
    }
    
}
